package db

import (
	"go-study/src/sjcc/models"
	"gorm.io/gorm"
	"log"
)

// 创建  手动关闭连接
func InsertSchool(db *gorm.DB, school models.School) {
	connection, err := db.DB()
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
	}
	db.Create(&school)
	closeErr := connection.Close()
	if err != nil {
		log.Println("close collection is error" + closeErr.Error())
	}
}

// 批量保存
func BatchInsertSchool(db *gorm.DB, schools []models.School) bool {
	connection, err := db.DB()
	if err != nil {
		log.Fatalf("failed to connect database: %v", err)
		return false
	}
	db.Create(&schools)
	closeErr := connection.Close()
	if err != nil {
		log.Println("close collection is error" + closeErr.Error())
		return false
	}
	return true
}

// 查询
func QuerySchool(db *gorm.DB) {
	// 读取
	var schools []models.School
	db.Find(&schools)
}

// 更新
func UpdateSchool(db *gorm.DB, school models.School) {
	// 更新
	db.Model(&school).Updates(models.School{})
}

// 删除
func DeleteSchool(db *gorm.DB, school models.School) {
	// 删除
	db.Delete(&models.School{}, 1)
}
